﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class NoticController : MyControllerBase
    {
        NoticManager manager;
        NoticFetcher fetcher;
        public NoticController() : base()
        {
            this.manager = new NoticManager();
            this.fetcher = new NoticFetcher();
        }
        [HttpGet]
        [Authorize]
        public AsmArray<NoticModel> List(int pos, int size)
        {
            AsmArray<NoticModel> result = new AsmArray<NoticModel>();
            if (profile == null)
            {
                LogHelper.Err(this.GetType(), "LIST:系统内部异常，Profile为空");
                result.Message = "系统内部异常";
                result.Result = AmsCode.InnError;
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
            else
            {
                LogHelper.Log(this.GetType(), String.Format("LIST:用户{0}:{1}正在列出功能清单", profile.UserId, RequestIp));
                int count = 0;
                var data = fetcher.ListByReceiver(profile.UserId, pos, size,out count);
                result.Data = data;
                result.Cnt = count;
                result.Message = "操作成功";
                result.Result = AmsCode.Succ;
            }
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));

            //DBHelper.Commit(tranCode, result.Result != AmsCode.Succ); 
            DBHelper.Commit(true);//只读类接口任何时候都要求commit,因为有可能产生数据库操作记录
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Create)]
        public AsmObject<NoticModel> Set(NoticModel noticModel)
        {
            if (noticModel == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<NoticModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(noticModel), RequestIp));
            var result = manager.Set(noticModel);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit(true);
            return result;
        }

    }
}
